package com.lxt.controller;

import com.lxt.common.HttpResult;
import com.lxt.common.util.CookieUtils;
import com.lxt.common.util.JsonUtils;
import com.lxt.pojo.Users;
import com.lxt.pojo.VO.UsersVO;
import com.lxt.resource.FileResource;
import com.lxt.service.FdfsService;
import com.lxt.service.OSSService;
import com.lxt.service.center.CenterUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author kevin.lv
 * @date 2023/3/6 13:55
 */
@RestController
@RequestMapping("fdfs")
public class CenterUserController extends BaseController {

    @Autowired
    private FdfsService fdfsService;

    @Autowired
    private OSSService ossService;

    @Autowired
    private FileResource fileResource;

    @Autowired
    private CenterUserService centerUserService;


    /**
     * fastDFS方式存储文件
     *
     * @param userId
     * @param file
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadFace")
    public HttpResult uploadFace(
            String userId,
            MultipartFile file,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        String path = "";
        //开始文件上传
        if (file != null) {
            //获得文件上传的文件名称
            String fileName = file.getOriginalFilename();
            if (StringUtils.isNotBlank(fileName)) {
                //face-{userId}.png  文件重命名 xxx-face.png -> ["xxx-face","png"]
                String[] fileNameArr = fileName.split("\\.");

                //获取文件后缀名
                String suffix = fileNameArr[fileNameArr.length - 1];

                if (!suffix.equalsIgnoreCase("png") &&
                        !suffix.equalsIgnoreCase("jpg") &&
                        !suffix.equalsIgnoreCase("jpeg")) {
                    return HttpResult.errorMsg("图片格式不正确!");
                }
                path = fdfsService.upload(file, suffix);
                System.out.println(path);
            }

        } else {
            return HttpResult.errorMsg("文件不能为空!");
        }

        if (StringUtils.isNotBlank(path)) {
            //获取图片服务地址
            String imageServerUrl = fileResource.getHost();
            String finalUserFaceUrl = imageServerUrl + path;

            //更新用户头像到数据库
            Users userResult = centerUserService.updateUserFace(userId, finalUserFaceUrl);
            UsersVO usersVO = conventUsersVO(userResult);

            CookieUtils.setCookie(request, response, "user",
                    JsonUtils.objectToJson(usersVO), true);

        } else {
            return HttpResult.errorMsg("上传头像失败");
        }
        return HttpResult.ok();
    }


    /**
     * 阿里云OSS上传文件
     *
     * @param userId
     * @param file
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadFaceOSS")
    public HttpResult uploadFaceOSS(
            String userId,
            MultipartFile file,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        String path = "";
        //开始文件上传
        if (file != null) {
            //获得文件上传的文件名称
            String fileName = file.getOriginalFilename();
            if (StringUtils.isNotBlank(fileName)) {
                //face-{userId}.png  文件重命名 xxx-face.png -> ["xxx-face","png"]
                String[] fileNameArr = fileName.split("\\.");

                //获取文件后缀名
                String suffix = fileNameArr[fileNameArr.length - 1];

                if (!suffix.equalsIgnoreCase("png") &&
                        !suffix.equalsIgnoreCase("jpg") &&
                        !suffix.equalsIgnoreCase("jpeg")) {
                    return HttpResult.errorMsg("图片格式不正确!");
                }
                path = ossService.uploadOSS(file, userId, suffix);
                System.out.println(path);
            }

        } else {
            return HttpResult.errorMsg("文件不能为空!");
        }

        if (StringUtils.isNotBlank(path)) {

            //获取图片服务地址
            String finalUserFaceUrl = fileResource.getOssHost() + path;

            //更新用户头像到数据库
            Users userResult = centerUserService.updateUserFace(userId, finalUserFaceUrl);
            UsersVO usersVO = conventUsersVO(userResult);

            CookieUtils.setCookie(request, response, "user",
                    JsonUtils.objectToJson(usersVO), true);

        } else {
            return HttpResult.errorMsg("上传头像失败");
        }
        return HttpResult.ok();
    }
}
